import { requestClient } from '#/api/request';
import type { PagedRequest, PagedList as PagedResult } from '#/api/types';

/**
 * 用户查询参数
 */
export interface UserQueryParams extends PagedRequest {
  userName?: string;
  email?: string;
  phoneNumber?: string;
  status?: number;
  userType?: number;
  startTime?: string;
  endTime?: string;
  roleId?: string;
  keyword?: string;
}

/**
 * 用户列表项
 */
export interface UserItem {
  id: string;
  userName: string;
  realName: string;
  nickName: string;
  email: string;
  emailConfirmed: boolean;
  phoneNumber: string;
  phoneNumberConfirmed: boolean;
  twoFactorEnabled: boolean;
  lockoutEnd: string | null;
  lockoutEnabled: boolean;
  accessFailedCount: number;
  avatarUrl: string;
  createdTime: string;
  lastLoginTime: string | null;
  lastLoginIp: string;
  status: number;
  userType: number;
  roles: string[];
}

/**
 * 用户详情
 */
export interface UserDetail extends UserItem {
  gender: number;
  birthDate: string | null;
  biography: string;
  requiresPasswordReset: boolean;
  lastPasswordChangedTime: string | null;
  roleIds: string[];
}

/**
 * 创建用户参数
 */
export interface UserCreateParams {
  userName: string;
  password: string;
  confirmPassword: string;
  email?: string;
  phoneNumber?: string;
  realName?: string;
  nickName?: string;
  gender?: number;
  birthDate?: string;
  avatarUrl?: string;
  biography?: string;
  userType?: number;
  status?: number;
  roleIds?: string[];
}

/**
 * 更新用户参数
 */
export interface UserUpdateParams {
  id: string;
  email?: string;
  phoneNumber?: string;
  realName?: string;
  nickName?: string;
  gender?: number;
  birthDate?: string;
  avatarUrl?: string;
  biography?: string;
  userType?: number;
  status?: number;
  roleIds?: string[];
  requiresPasswordReset?: boolean;
}

/**
 * 重置密码参数
 */
export interface ResetPasswordParams {
  userId: string;
  newPassword: string;
  confirmPassword: string;
}

/**
 * 更改密码参数
 */
export interface ChangePasswordParams {
  userId: string;
  currentPassword: string;
  newPassword: string;
  confirmPassword: string;
}

/**
 * 用户状态更新参数
 */
export interface UserStatusUpdateParams {
  userId: string;
  status: number;
}

/**
 * 角色分配参数
 */
export interface UserRoleAssignParams {
  userId: string;
  roleIds: string[];
}

/**
 * 分页查询用户
 */
export function getUserList(params: UserQueryParams) {
  return requestClient.get<PagedResult<UserItem>>('/users', { params });
}

/**
 * 获取用户详情
 */
export function getUserDetail(id: string) {
  return requestClient.get<UserDetail>(`/users/${id}`);
}

/**
 * 创建用户
 */
export function createUser(params: UserCreateParams) {
  return requestClient.post<string>('/users', params);
}

/**
 * 更新用户
 */
export function updateUser(id: string, params: UserUpdateParams) {
  return requestClient.put<void>(`/users/${id}`, params);
}

/**
 * 删除用户
 */
export function deleteUser(id: string) {
  return requestClient.delete<void>(`/users/${id}`);
}

/**
 * 批量删除用户
 */
export function batchDeleteUsers(ids: string[]) {
  return requestClient.post<number>('/users/batch-delete', ids);
}

/**
 * 重置密码
 */
export function resetPassword(params: ResetPasswordParams) {
  return requestClient.post<void>('/users/reset-password', params);
}

/**
 * 修改密码
 */
export function changePassword(params: ChangePasswordParams) {
  return requestClient.post<void>('/users/change-password', params);
}

/**
 * 更新用户状态
 */
export function updateUserStatus(params: UserStatusUpdateParams) {
  return requestClient.put<void>('/users/status', params);
}

/**
 * 为用户分配角色
 */
export function assignRolesToUser(params: UserRoleAssignParams) {
  return requestClient.put<void>('/users/roles', params);
}

/**
 * 检查用户名是否存在
 */
export function checkUserNameExists(userName: string, excludeUserId?: string) {
  return requestClient.get<boolean>('/users/check-username', {
    params: { userName, excludeUserId },
  });
}

/**
 * 检查邮箱是否存在
 */
export function checkEmailExists(email: string, excludeUserId?: string) {
  return requestClient.get<boolean>('/users/check-email', {
    params: { email, excludeUserId },
  });
}

/**
 * 检查手机号是否存在
 */
export function checkPhoneNumberExists(phoneNumber: string, excludeUserId?: string) {
  return requestClient.get<boolean>('/users/check-phone', {
    params: { phoneNumber, excludeUserId },
  });
}

/**
 * 上传用户头像
 */
export function uploadAvatar(formData: FormData) {
  return requestClient.post<string>('/users/upload-avatar', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}

/**
 * 导出用户数据
 */
export function exportUsers(params: UserQueryParams) {
  return requestClient.get<Blob>('/users/export', {
    params,
    responseType: 'blob'
  });
}

/**
 * 导入用户数据
 */
export function importUsers(formData: FormData) {
  return requestClient.post<{ total: number; success: number; fail: number }>('/users/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}
